package view.Core;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import method.gridviewPhoto.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.widget.AdapterView.*;

public class tab_photoGridViewActivity extends Activity {
	private String treeImgID, url,treeIDValue;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photogridview);

        
		Bundle bunde = this.getIntent().getExtras();
		treeImgID = bunde.getString("treeImgID");
		try {
			
			String inputTreeID = "inputTreeID="+treeImgID;
			/*String url = getResources().getString(R.string.serverURL)
							+getResources().getString(R.string.ByTreeIDPHP)
							+inputTreeID;*/
			url = getResources().getString(R.string.serverURL)+"getJsonByQR.php?"+inputTreeID;
        	//getJsonByTreeID.php
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			String reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply += line;
			}
			reader.close();
			try {

				JSONArray json = new JSONArray(reply);

				JSONArray jsonArrayB = new JSONArray(json.getString(0));
				String getField = jsonArrayB.getString(0);
				// JSONObject jsonArray = new JSONObject(treeData);
				JSONObject jsonField = new JSONObject(getField);
				treeIDValue = jsonField.getString("treeID");

			} catch (JSONException e) {
				//treeDataTextView.setText("JSONException");
			}
		} catch (Exception e) {
			//treeDataTextView.setText("Please Check network connection");
			//treeDataTextView.setText(url);
		}
        
        String treeID = treeIDValue;
		//String treeID = "T0002";
		String URLlink = getResources().getString(R.string.serverURL);
		String URLlinkB = getResources().getString(R.string.serverURLTreeWeb);
		
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this, treeID, URLlink, URLlinkB));
      
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Toast.makeText(gridViewActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            	 Toast.makeText(tab_photoGridViewActivity.this, "" + ImageAdapter.getSelectPhotoURL(position), Toast.LENGTH_SHORT).show();
		            Intent intent = new Intent();
		            intent.setClass(tab_photoGridViewActivity.this, treesImgShow.class);
		            Bundle bundle = new Bundle();
		            bundle.putString("treePhotoURL",""+ImageAdapter.getSelectPhotoURL(position));
		            intent.putExtras(bundle);
		            startActivity(intent);
            }
        });
    }
}